/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.willy.ratingbar;

import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorGroup;
import ohos.agp.animation.AnimatorProperty;

import ohos.agp.components.*;
import ohos.app.Context;
import ohos.multimodalinput.event.TouchEvent;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * scaratingbar
 *
 * @since 2021-03-26
 */
public class ScaleRatingBar extends DirectionalLayout implements Component.TouchEventListener {
    private DirectionalLayout rotalayout;
    private Image img1;
    private Image img2;
    private Image img3;
    private Image img4;
    private Image img5;
    private DirectionalLayout dire1;
    private DirectionalLayout dire2;
    private DirectionalLayout dire3;
    private DirectionalLayout dire4;
    private DirectionalLayout dire5;
    private boolean isClick = false;
    private boolean isMove = false;
    private float firstaxisX = 0f;
    private int post = 0;
    private List<Animator> mAnimatorList = new ArrayList<>();
    private HashMap<Image, Animator> mHashMap = new HashMap<>();
    private int isAmtion = 0;

    /**
     * 构造方法
     *
     * @param context 上下文
     */
    public ScaleRatingBar(Context context) {
        super(context);
        initview(context);
    }

    /**
     * 构造方法2
     *
     * @param context q
     * @param attrSet q
     */
    public ScaleRatingBar(Context context, AttrSet attrSet) {
        super(context, attrSet);
        initview(context);
    }

    /**
     * qee
     *
     * @param context   q
     * @param attrSet   q
     * @param styleName q
     */
    public ScaleRatingBar(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
        initview(context);
    }

    private void initview(Context context) {
        Component root = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_scalelayout, this, true);
        rotalayout = (DirectionalLayout) root.findComponentById(ResourceTable.Id_rotalayout);
        rotalayout.setTouchEventListener(this::onTouchEvent);
        dire1 = (DirectionalLayout) root.findComponentById(ResourceTable.Id_dire1);
        dire2 = (DirectionalLayout) root.findComponentById(ResourceTable.Id_dire2);
        dire3 = (DirectionalLayout) root.findComponentById(ResourceTable.Id_dire3);
        dire4 = (DirectionalLayout) root.findComponentById(ResourceTable.Id_dire4);
        dire5 = (DirectionalLayout) root.findComponentById(ResourceTable.Id_dire5);
        img1 = (Image) root.findComponentById(ResourceTable.Id_scaimg1);
        img2 = (Image) root.findComponentById(ResourceTable.Id_scaimg2);
        img3 = (Image) root.findComponentById(ResourceTable.Id_scaimg3);
        img4 = (Image) root.findComponentById(ResourceTable.Id_scaimg4);
        img5 = (Image) root.findComponentById(ResourceTable.Id_scaimg5);

        img1.setPixelMap(ResourceTable.Media_filled);
        img2.setPixelMap(ResourceTable.Media_filled);
        img3.setPixelMap(ResourceTable.Media_filled);
        img4.setPixelMap(ResourceTable.Media_filled);
        post = NumberUntil.getAnInt4();
        isClick = true;
    }

    @Override
    public boolean onTouchEvent(Component component, TouchEvent touchEvent) {
        switch (touchEvent.getAction()) {
            case TouchEvent.PRIMARY_POINT_DOWN:
                firstaxisX = touchEvent.getPointerPosition(0).getX();
                setclick(firstaxisX);
                break;
            case TouchEvent.POINT_MOVE:
                float moveX = touchEvent.getPointerPosition(0).getX();
                float al = moveX - firstaxisX;
                if (al > NumberUntil.getAnInt10() || al < -NumberUntil.getAnInt10()) {
                    setrigat(moveX);
                }
                break;
            case TouchEvent.CANCEL:
            case TouchEvent.PRIMARY_POINT_UP:
            default:
                break;
        }
        return true;
    }

    private void setrigat(float axisX) {
        if (dire1.getContentPositionX() > axisX) {
            if (isMove) {
                isMove = false;
                img1.setPixelMap(ResourceTable.Media_empty);
            } else {
                isMove = true;
                img1.setPixelMap(ResourceTable.Media_filled);
                setpostamtion(1);
                post = 1;
            }
        } else if (dire2.getContentPositionX() > axisX) {
            if (isMove) {
                isMove = false;
                img2.setPixelMap(ResourceTable.Media_empty);
                img3.setPixelMap(ResourceTable.Media_empty);
                img4.setPixelMap(ResourceTable.Media_empty);
                img5.setPixelMap(ResourceTable.Media_empty);
            } else {
                isMove = true;
                img1.setPixelMap(ResourceTable.Media_filled);
                img2.setPixelMap(ResourceTable.Media_filled);
                setpostamtion(NumberUntil.getAnInt2());
                post = NumberUntil.getAnInt2();
            }
        } else if (dire3.getContentPositionX() > axisX) {
            if (isMove) {
                isMove = false;
                img3.setPixelMap(ResourceTable.Media_empty);
                img4.setPixelMap(ResourceTable.Media_empty);
                img5.setPixelMap(ResourceTable.Media_empty);
            } else {
                isMove = true;
                img1.setPixelMap(ResourceTable.Media_filled);
                img2.setPixelMap(ResourceTable.Media_filled);
                img3.setPixelMap(ResourceTable.Media_filled);
                setpostamtion(NumberUntil.getAnInt3());
                post = NumberUntil.getAnInt3();
            }

        } else if (dire4.getContentPositionX() > axisX) {
            setrigatFour();
        } else if (dire5.getContentPositionX() > axisX) {
            setrigatFive();
        }
    }

    private void setrigatFour() {
        if (isMove) {
            isMove = false;
            img4.setPixelMap(ResourceTable.Media_empty);
            img5.setPixelMap(ResourceTable.Media_empty);

        } else {
            isMove = true;
            img1.setPixelMap(ResourceTable.Media_filled);
            img2.setPixelMap(ResourceTable.Media_filled);
            img3.setPixelMap(ResourceTable.Media_filled);
            img4.setPixelMap(ResourceTable.Media_filled);
            setpostamtion(NumberUntil.getAnInt4());
            post = NumberUntil.getAnInt4();
        }
    }

    private void setrigatFive() {
        if (isMove) {
            isMove = false;
            img5.setPixelMap(ResourceTable.Media_empty);
        } else {
            isMove = true;
            img1.setPixelMap(ResourceTable.Media_filled);
            img2.setPixelMap(ResourceTable.Media_filled);
            img3.setPixelMap(ResourceTable.Media_filled);
            img4.setPixelMap(ResourceTable.Media_filled);
            img5.setPixelMap(ResourceTable.Media_filled);
            post = NumberUntil.getAnInt5();
            setpostamtion(NumberUntil.getAnInt5());
        }
    }

    private void setclick(float axisX) {
        if (dire1.getContentPositionX() - NumberUntil.getAnInt115() < axisX
                && axisX < dire1.getContentPositionX() + NumberUntil.getAnInt115()) {
            if (isClick) {
                isClick = false;
                img1.setPixelMap(ResourceTable.Media_empty);
            } else {
                isClick = true;
                img1.setPixelMap(ResourceTable.Media_filled);
                setpostamtion(1);
                post = 1;
            }
            img2.setPixelMap(ResourceTable.Media_empty);
            img3.setPixelMap(ResourceTable.Media_empty);
            img4.setPixelMap(ResourceTable.Media_empty);
            img5.setPixelMap(ResourceTable.Media_empty);
        } else if (dire2.getContentPositionX() - NumberUntil.getAnInt115() < axisX
                && axisX < dire2.getContentPositionX() + NumberUntil.getAnInt115()) {
            if (isClick) {
                isClick = false;
                img1.setPixelMap(ResourceTable.Media_empty);
                img2.setPixelMap(ResourceTable.Media_empty);
            } else {
                isClick = true;
                img1.setPixelMap(ResourceTable.Media_filled);
                img2.setPixelMap(ResourceTable.Media_filled);
                setpostamtion(NumberUntil.getAnInt2());
                post = NumberUntil.getAnInt2();
            }
            img3.setPixelMap(ResourceTable.Media_empty);
            img4.setPixelMap(ResourceTable.Media_empty);
            img5.setPixelMap(ResourceTable.Media_empty);
        } else if (dire3.getContentPositionX() - NumberUntil.getAnInt115() < axisX
                && axisX < dire3.getContentPositionX() + NumberUntil.getAnInt115()) {
            setThere();
        } else if (dire4.getContentPositionX() - NumberUntil.getAnInt115() < axisX
                && axisX < dire4.getContentPositionX() + NumberUntil.getAnInt115()) {
            setFour();
        } else if (dire5.getContentPositionX() - NumberUntil.getAnInt115() < axisX
                && axisX < dire5.getContentPositionX() + NumberUntil.getAnInt115()) {
            setfive();
        }
    }

    private void setThere() {
        if (isClick) {
            isClick = false;
            img1.setPixelMap(ResourceTable.Media_empty);
            img2.setPixelMap(ResourceTable.Media_empty);
            img3.setPixelMap(ResourceTable.Media_empty);
        } else {
            isClick = true;
            img1.setPixelMap(ResourceTable.Media_filled);
            img2.setPixelMap(ResourceTable.Media_filled);
            img3.setPixelMap(ResourceTable.Media_filled);
            setpostamtion(NumberUntil.getAnInt3());
            post = NumberUntil.getAnInt3();
        }
        img4.setPixelMap(ResourceTable.Media_empty);
        img5.setPixelMap(ResourceTable.Media_empty);
    }

    private void setFour() {
        if (isClick) {
            isClick = false;
            img1.setPixelMap(ResourceTable.Media_empty);
            img2.setPixelMap(ResourceTable.Media_empty);
            img3.setPixelMap(ResourceTable.Media_empty);
            img4.setPixelMap(ResourceTable.Media_empty);
        } else {
            isClick = true;
            img1.setPixelMap(ResourceTable.Media_filled);
            img2.setPixelMap(ResourceTable.Media_filled);
            img3.setPixelMap(ResourceTable.Media_filled);
            img4.setPixelMap(ResourceTable.Media_filled);
            setpostamtion(NumberUntil.getAnInt4());
            post = NumberUntil.getAnInt4();
        }
        img5.setPixelMap(ResourceTable.Media_empty);
    }

    private void setfive() {
        if (isClick) {
            isClick = false;
            img1.setPixelMap(ResourceTable.Media_empty);
            img2.setPixelMap(ResourceTable.Media_empty);
            img3.setPixelMap(ResourceTable.Media_empty);
            img4.setPixelMap(ResourceTable.Media_empty);
            img5.setPixelMap(ResourceTable.Media_empty);
        } else {
            isClick = true;
            img1.setPixelMap(ResourceTable.Media_filled);
            img2.setPixelMap(ResourceTable.Media_filled);
            img3.setPixelMap(ResourceTable.Media_filled);
            img4.setPixelMap(ResourceTable.Media_filled);
            img5.setPixelMap(ResourceTable.Media_filled);
            setpostamtion(NumberUntil.getAnInt5());
            post = NumberUntil.getAnInt5();
        }
    }

    public int getPost() {
        return post;
    }

    private Image getImg(int pst) {
        Image image = null;
        if (pst == 1) {
            image = img1;
        } else if (pst == NumberUntil.getAnInt2()) {
            image = img2;
        } else if (pst == NumberUntil.getAnInt3()) {
            image = img3;
        } else if (pst == NumberUntil.getAnInt4()) {
            image = img4;
        } else if (pst == NumberUntil.getAnInt5()) {
            image = img5;
        }
        return image;
    }

    private void setChanged(AnimatorGroup animator, Image image) {
        AnimatorGroup animatorSet = animator;
        Image finalImage = image;
        animatorSet.setStateChangedListener(new Animator.StateChangedListener() {
            @Override
            public void onStart(Animator animator) {
            }

            @Override
            public void onStop(Animator animator) {
                finalImage.setRotation(0);
                finalImage.setScaleX(1.0f);
                finalImage.setScaleY(1.0f);
            }

            @Override
            public void onCancel(Animator animator) {
                finalImage.setRotation(0);
                finalImage.setScaleX(1.0f);
                finalImage.setScaleY(1.0f);
            }

            @Override
            public void onEnd(Animator animator) {
                finalImage.setRotation(0);
                finalImage.setScaleX(1.0f);
                finalImage.setScaleY(1.0f);
            }

            @Override
            public void onPause(Animator animator) {
            }

            @Override
            public void onResume(Animator animator) {
            }
        });
    }

    /**
     * 设置星星的动画
     *
     * @param pst 第几个星星
     */
    public void setpostamtion(int pst) {
        if (pst == 0) {
            return;
        }
        Image image = getImg(pst);
        if (mHashMap.containsKey(image) && mHashMap.get(image).isRunning()) {
            return;
        }
        mAnimatorList.clear();
        if (isAmtion == 0) {
            mAnimatorList.addAll(getAnimator(image));
        } else {
            mAnimatorList.addAll(getScaleAnimator(image));
        }
        AnimatorGroup animatorSet = (AnimatorGroup) mHashMap.get(image);
        if (animatorSet == null) {
            animatorSet = new AnimatorGroup();
        }
        animatorSet.setDuration(0);
        AnimatorProperty[] a = new AnimatorProperty[mAnimatorList.size()];
        mAnimatorList.toArray(a);
        animatorSet.runParallel(a);
        Image finalImage = image;
        setChanged(animatorSet, finalImage);
        mHashMap.put(image, animatorSet);
        animatorSet.start();
    }

    private List<Animator> getScaleAnimator(Image image) {
        List<Animator> animatorList = new ArrayList<Animator>(NumberUntil.getAnInt2());
        AnimatorProperty animatorProperty = image.createAnimatorProperty();
        animatorProperty.reset();
        animatorProperty.scaleXFrom(NumberUntil.getAnfloat1d2()).scaleX(1.0f);
        animatorProperty.scaleYFrom(NumberUntil.getAnfloat1d2()).scaleY(1.0f);
        animatorProperty.setDuration(NumberUntil.getAnInt500());
        animatorProperty.setTarget(image);
        animatorList.add(animatorProperty);
        return animatorList;
    }

    private List<Animator> getAnimator(Image image) {
        List<Animator> animatorList = new ArrayList<Animator>(NumberUntil.getAnInt2());
        AnimatorProperty animatorProperty = image.createAnimatorProperty();
        animatorProperty.reset();
        animatorProperty.rotate(NumberUntil.getAnInt360());
        animatorProperty.setDuration(NumberUntil.getAnInt500());
        animatorProperty.setTarget(image);
        animatorList.add(animatorProperty);
        return animatorList;
    }

    /**
     * 设置当前动画
     *
     * @param pt q
     * @return q
     */
    public int setAmtionPost(int pt) {
        isAmtion = pt;
        return isAmtion;
    }
}
